package cn.shop.gateway.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * @author : 李一涵
 * @date : Created in 2021-12-24 17:26
 * @description :
 */
public class ScriptUtil {

    private static final Logger LOG = LoggerFactory.getLogger(ScriptUtil.class);

    private final static int SIZE = 10;

    private final static String TEMPLATE = "\n#IF\n" +
            "CheckAccountList ..\\QuestDiary\\购买领取\\%name%\\%count%.txt \n" +
            "#ACT\n" +
            "%code% + %count%\n" +
            "DelAccountList ..\\QuestDiary\\购买领取\\%name%\\%count%.txt \n" +
            "SENDMSG 1  ★玩家<$USERNAME>使用本服寄售系统，成功购买%name%\n" +
            "goto @领取%name%\n";

    private final static String SCRIPT_VALUE = "[@领取%name%]\n" +
            "{\n" +
            "{}\n" +
            "\n" +
            "#ELSESAY\n" +
            "您当前还没有代领取%name%或已领取成功,请查看自己的%name%数量!\n" +
            "<返回/@main>\n" +
            "}";

    public static void createBuyScript(File file, String name, String code, long max, long min, long step) {
        StringBuilder buffer = new StringBuilder();
        File parent = file.getParentFile();
        long s = 1;
        while (s <= max) {
            if (s == max) {
                String count = String.valueOf(s);
                File c = new File(parent, count + ".txt");
                if (!c.exists()) {
                    try {
                        c.createNewFile();
                    } catch (Exception ignored) {}
                }
                buffer.append(TEMPLATE.replaceAll("%count%", count)
                        .replaceAll("%code%", code).replaceAll("%name%", name));
            } else {
                for(int i = 1; i < SIZE; i++) {
                    String count = String.valueOf(s * i);
                    File c = new File(parent, count + ".txt");
                    if (!c.exists()) {
                        try {
                            c.createNewFile();
                        } catch (Exception ignored) {}
                    }
                    buffer.append(TEMPLATE.replaceAll("%count%", count)
                            .replaceAll("%code%", code).replaceAll("%name%", name));
                }
            }
            s *= step;
        }
        String content = StrUtil.format(SCRIPT_VALUE, buffer.toString()).replaceAll("%name%", name);
        FileUtil.writeString(content, file, CharsetUtil.CHARSET_GBK);
    }

    public static void createSellScript(File file, String name, String code, int ratio, long max, long pid, String url) {
        try {
            File template = new File("template/template.txt");
            String content = FileUtil.readString(template, CharsetUtil.CHARSET_UTF_8);
            FileUtil.writeString(content.replaceAll("%name%", name)
                    .replaceAll("%url%", url)
                    .replaceAll("%pid%", String.valueOf(pid))
                    .replaceAll("%code%", code)
                    .replaceAll("%min%", String.valueOf(ratio))
                    .replaceAll("%ratio%", String.valueOf(ratio))
                    .replaceAll("%max%", String.valueOf(max)), file, CharsetUtil.CHARSET_GBK);
        } catch (Exception e) {
            LOG.warn("创建脚本错误", e);
        }
    }

}
